package main

import (
	"bufio"
	"fmt"
	"net"
)

/*
   Author: mahaiyuan
   Date: 2021/4/19 7:53 下午
   Description:
*/

func main() {
	listen, err := net.Listen("tcp", "127.0.0.1:9999")
	if err != nil {
		fmt.Println("listen failed.", err)
		return
	}

	for true {
		conn, err := listen.Accept() //建立连接
		if err != nil {
			fmt.Println("accept failed.", err)
			continue
		}
		go process(conn)
	}
}

func process(conn net.Conn) {
	defer conn.Close()
	for true {
		reader := bufio.NewReader(conn)
		var buf [128]byte
		read, err := reader.Read(buf[:])
		if err != nil {
			fmt.Println("read data from client fail.", err)
			break
		}
		data := string(buf[:read])
		fmt.Println("read data is :", data)
		conn.Write([]byte(data))	//向客户端输出内容
	}
}
